package com.jml.栈;


import java.util.Stack;

/**
 * @author:jml
 * @date: 2019/5/16 10:24
 * @description：
 */
public class 栈匹配括号 {

    public static void main(String[] args) {
        System.out.println(checkValid("()[]{}[]]"));
    }
    private static boolean checkValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i<s.length(); i++) {
            char c = s.charAt(i);
            if(c=='{'|| c=='[' || c=='('){
                stack.push(c);
            }else if(c=='}'|| c==']' || c==')'){
                if(stack.isEmpty()){
                    return false;
                }
                Character pop = stack.pop();
                if(pop=='{'&&c!='}'){ 
                    return false;
                }
                if(pop=='('&&c!=')'){
                    return false;
                }
                if(pop=='['&&c!=']'){
                    return false;
                }
            }
        }
        return  stack.isEmpty();
    }
}
